package com.dhcc.cdc.sys.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.entity.CdcDictionaryEntity;
import com.dhcc.cdc.busi.service.ICdcDictionaryService;
import com.dhcc.cdc.sys.dao.DictionaryMapper;
import com.dhcc.cdc.sys.entity.DictionaryEntity;
import com.dhcc.cdc.sys.entity.DictypeEntity;
import com.dhcc.cdc.sys.service.IDictionaryService;
import com.dhcc.cdc.sys.service.IDictypeService;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * 系统字典表数据服务层接口
 * 
 * @ClassName: DictionaryServiceImpl
 * @Description: TODO
 * @author jumper
 * @date 2021-04-16 17:11:56
 */
@Service("dictionaryService")
public class DictionaryServiceImpl extends BaseServiceImpl<DictionaryMapper, DictionaryEntity> implements IDictionaryService {
    @Autowired
    IDictypeService dicTypeSrv;

    @Autowired
    private ICdcDictionaryService cdcDictionaryService;

	@Override
	public List<DictionaryEntity> page(Page<DictionaryEntity> page, Map<String, Object> map) {
		return this.baseMapper.page(page, map);
	}
	@Override
	public List<DictionaryEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}
    @Override
    public List<DictionaryEntity> queryByTypeCode(String productCode,String typeCode){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("productCode", productCode);
        map.put("code", typeCode);
        DictypeEntity dicType = this.dicTypeSrv.list(map).get(0);

        if(dicType == null) return null;
        String dictypeId = String.valueOf(dicType.getId());
        map.clear();
        map.put("dictypeId", dictypeId);
        return this.baseMapper.list(map);
    }

    @Override
    public DictionaryEntity queryByTypeCodeAndCode(String dataCode, String dicTypeCode, String typeCode) {
        DictionaryEntity dictionaryEntity = null;
        List<DictionaryEntity> dictionaryEntities = this.queryByTypeCode(dataCode, dicTypeCode);
        for (DictionaryEntity dictionaryEntity1 : dictionaryEntities){
            if (Objects.equals(typeCode, dictionaryEntity1.getCode())){
                dictionaryEntity = dictionaryEntity1;
            }
        }
        return dictionaryEntity;
    }

    @Override
    public List<DictionaryEntity> pageByProductCodeAndTypeCode(Page<DictionaryEntity> page, Map<String, Object> params, String productCode, String typeCode) {
        Object ctDic = params.get("ctDic");
        if (StringUtils.isEmpty(ctDic)){
            return null;
        }

        Object compare = params.get("compare");
        String s = String.valueOf(compare);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("productCode", productCode);
        map.put("code", typeCode);
        DictypeEntity dicType = this.dicTypeSrv.list(map).get(0);
        if(dicType == null) return null;
        String dictypeId = String.valueOf(dicType.getId());
        params.put("dictypeId", dictypeId);
        Long aLong = Long.valueOf(String.valueOf(ctDic));
        List<DictionaryEntity> page1 = new ArrayList<>();

        if (Objects.equals("match", s)){
            params.put("ctDic", aLong);
            page1 = this.baseMapper.page(page, params);
            for (DictionaryEntity dictionaryEntity : page1){
                DictypeEntity dictypeEntity = dicTypeSrv.selectById(dictionaryEntity.getDictypeId());
                dictionaryEntity.setDictypeDesc(dictypeEntity.getDescription());
                dictionaryEntity.setIsCheck(true);
            }
        }else if (Objects.equals("noMatch", s)){
            params.put("ctDic", aLong);
            List<Long> matchIds = this.baseMapper.selectMatchIds(params);
            params.put("ctDic", "");
            page1 = this.baseMapper.pageExcludeMatch(page, params, matchIds);
            for (DictionaryEntity dictionaryEntity : page1){
                DictypeEntity dictypeEntity = dicTypeSrv.selectById(dictionaryEntity.getDictypeId());
                dictionaryEntity.setDictypeDesc(dictypeEntity.getDescription());
            }
        }else if (Objects.equals("all", s)){
            params.put("ctDic", "");
            page1 = this.baseMapper.page(page, params);
            for (DictionaryEntity dictionaryEntity : page1){
                DictypeEntity dictypeEntity = dicTypeSrv.selectById(dictionaryEntity.getDictypeId());
                dictionaryEntity.setDictypeDesc(dictypeEntity.getDescription());
                if (Objects.equals(aLong, dictionaryEntity.getCtDic())){
                    dictionaryEntity.setIsCheck(true);
                }
            }
        }

        return page1;

        /*//已对照-yes，未对照-no，全部
        List<DictionaryEntity> collect = new ArrayList<>();
        if("yes".equals(compare)){
            collect = page1.stream().filter(item -> (item.getIsCheck() != null && item.getIsCheck() == true)).collect(Collectors.toList());
            return collect;
        }else if("no".equals(compare)){
            collect = page1.stream().filter(item -> (item.getIsCheck() == null || "".equals(item.getIsCheck()))).collect(Collectors.toList());
            return collect;
        }else {
            return page1;
        }*/
    }

    @Override
    public void compare(Long cdcSysDicId, Long cdcCtDicId) {
        DictionaryEntity dictionaryEntity = this.baseMapper.selectById(cdcSysDicId);
        if (!StringUtils.isEmpty(dictionaryEntity)){
            dictionaryEntity.setCtDic(cdcCtDicId);
        }
        this.baseMapper.updateById(dictionaryEntity);
    }

    @Override
    public void undo(Long cdcSysDicId, Long cdcCtDicId) {
        DictionaryEntity dictionaryEntity = this.baseMapper.selectById(cdcSysDicId);
        if (!StringUtils.isEmpty(dictionaryEntity)){
            dictionaryEntity.setCtDic(null);
        }
        this.baseMapper.updateById(dictionaryEntity);
    }

    @Override
    public CdcDictionaryEntity getMatchCtDic(String dataCode, String dicTypeCode, String typeCode) {
        DictionaryEntity dictionaryEntity1 = this.queryByTypeCodeAndCode(dataCode, dicTypeCode, typeCode);
        if (!StringUtils.isEmpty(dictionaryEntity1)){
            CdcDictionaryEntity dictionaryEntity = cdcDictionaryService.selectById(dictionaryEntity1.getCtDic());
            return dictionaryEntity;
        }else {
            return null;
        }
    }

    @Override
    public String getMatchCtDicCode(String dataCode, String dicTypeCode, String typeCode) {
	    String res = "";
        DictionaryEntity dictionaryEntity1 = this.queryByTypeCodeAndCode(dataCode, dicTypeCode, typeCode);
        if (!StringUtils.isEmpty(dictionaryEntity1)){
            CdcDictionaryEntity dictionaryEntity = cdcDictionaryService.selectById(dictionaryEntity1.getCtDic());
            if (!StringUtils.isEmpty(dictionaryEntity)){
                res = dictionaryEntity.getCode();
            }
        }
        return res;
    }

    @Override
    public List<Long> selectMatchDicsub() {
        return this.baseMapper.selectMatchDicsub();
    }
}